package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.TrainingRegistrationAddDto;
import com.njworkorder.Entity.TrainingRegistration;
import com.njworkorder.Service.TrainingRegistrationService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "培训登记接口")
@RestController
@RequestMapping("/training")
public class TrainingRegistrationController {

    private final TrainingRegistrationService trainingRegistrationService;
    public TrainingRegistrationController(TrainingRegistrationService trainingRegistrationService) {
        this.trainingRegistrationService = trainingRegistrationService;
    }

    @Operation(summary = "培训信息登记")
    @PostMapping("/addTraining")
    public Result<String> addTraining(@RequestBody TrainingRegistrationAddDto trainingRegistrationAddDto) {
        TrainingRegistration trainingRegistration = new TrainingRegistration();
        BeanUtils.copyProperties(trainingRegistrationAddDto, trainingRegistration);
        return trainingRegistrationService.save(trainingRegistration) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }

    @Operation(summary = "更具id删除培训登记信息")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {
        return trainingRegistrationService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败！");
    }

    @Operation(summary = "根据id更新培训登记信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@RequestBody TrainingRegistrationAddDto trainingRegistrationAddDto,@PathVariable("id") String id) {
        TrainingRegistration trainingRegistration = new TrainingRegistration();
        BeanUtils.copyProperties(trainingRegistrationAddDto, trainingRegistration);
        trainingRegistration.setId(id);
        return trainingRegistrationService.updateById(trainingRegistration) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败！");
    }

    @Operation(summary = "根据id查询培训登记信息")
    @GetMapping("/getById")
    public Result<TrainingRegistration> getById(@RequestParam("id") String id) {
        TrainingRegistration byId = trainingRegistrationService.getById(id);
        return ResultUtil.success(byId);
    }

    @Operation(summary = "获取培训登记列表信息")
    @GetMapping("/getList")
    public Result<Page<TrainingRegistration>> getList(@RequestParam("pageIndex") int pageIndex,
                                                      @RequestParam("pageSize") int pageSize,
                                                      @RequestParam(value = "theme",required = false) String theme,
                                                      @RequestParam(value = "type",required = false) String type,
                                                      @RequestParam(value = "sTime",required = false) String sTime,
                                                      @RequestParam(value = "eTime",required = false) String eTime) {
        LambdaQueryWrapper<TrainingRegistration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(theme),TrainingRegistration::getTheme, theme);
        queryWrapper.eq(StringUtils.isNotEmpty(type),TrainingRegistration::getType, type);
        queryWrapper.between(StringUtils.isNotEmpty(sTime),TrainingRegistration::getTrainingTime,sTime,eTime);
        Page<TrainingRegistration> page = trainingRegistrationService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(page);
    }


}
